package com.xyz.business.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.xyz.business.bean.User;
import com.xyz.business.bean.query.UserForm;
import com.xyz.business.dao.UserDao;
import com.xyz.business.response.UserQueryResponse;
import com.xyz.business.service.UserService;
import com.xyz.constant.XYZConstant;
import com.xyz.util.SEQNoHelper;
import com.xyz.util.SpellUtils;

/**
 * 用户管理服务类
 * 
 * @author xyzjh
 * 
 */
public class UserServiceImpl implements UserService {
	/**
	 * 日志类
	 */
	private static Logger log = Logger.getLogger(UserServiceImpl.class);
	/**
	 * 用户管理数据库访问类
	 */
	private UserDao userDao;

	public UserQueryResponse query(UserForm form) {
		log.info(" query begin...");
		UserQueryResponse response = new UserQueryResponse();

		try {
			response = userDao.queryUser(form);
		} catch (Exception e) {
			log.info(" Catch one exception:" + e);
			List<User> userList = new ArrayList<User>();
			response.setReturnCode("0001");
			response.setRowCount(XYZConstant.NUM_0);
			response.setUserList(userList);
		}
		return response;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public int queryUserByName(String loginname) throws SQLException {

		return userDao.queryUserByName(loginname);
	}

	@Override
	public int login(User user) throws SQLException {
		// TODO Auto-generated method stub
		return userDao.login(user);
	}

	@Override
	public void insertUser(User user) throws SQLException {
		user.setUid(SEQNoHelper.getSeqNo(XYZConstant.USER_FLAG));

		/**
		 * 如果用户名为空,则根据姓名生成用户名
		 */
		if (StringUtils.isBlank(user.getLoginname())) {
			String loginname = SpellUtils.getInstance().getPinyinByHanyu(
					user.getName());
			user.setLoginname(loginname);
		}
		user.setIshide(XYZConstant.SHOW_USER);
		userDao.insertUser(user);
	}

	@Override
	public List<User> query() throws SQLException {
		// TODO Auto-generated method stub
		return userDao.query();
	}

	@Override
	public User queryUser(String loginname) throws SQLException {
		// TODO Auto-generated method stub
		return userDao.queryUser(loginname);
	}

	@Override
	public int queryUserCount(User user) throws SQLException {
		// TODO Auto-generated method stub
		return userDao.queryUserCount(user);
	}

	@Override
	public List<User> query(User user, int pageNow, int pageSize)
			throws SQLException {
		// TODO Auto-generated method stub
		return userDao.query(user, pageNow, pageSize);
	}

	/**
	 * 更新用户
	 * 
	 * @see com.xyz.business.service.UserService#updateUser(com.xyz.business.bean.User)
	 */
	@Override
	public int updateUser(User user) throws SQLException {
		user.setIshide(XYZConstant.SHOW_USER);
		return userDao.updateUser(user);
	}

	@Override
	public void hideUser(String uid) throws SQLException {
		userDao.hideUser(uid);

	}

}
